/**
 * @Auther: ChenforCode
 * @Date: 2018/9/11 23:50
 * @Description:
 */
package cn.chenforcode.dao;

import cn.chenforcode.entity.BookAdmin;
import oracle.jdbc.OracleType;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.SessionFactoryUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.List;

@Repository
@Transactional
public class BookAdminDAO {
    @Autowired
    private SessionFactory sessionFactory;

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * @return void
     * @Author ChenforCode
     * @Description //TODO 增加一个图书管理员
     * @Date 23:52 2018/9/11
     * @Param [bookAdmin]
     **/
    public void addBookAdmin(BookAdmin bookAdmin) {
        this.getSession().save(bookAdmin);
    }

    /**
     * @return int
     * @Author ChenforCode
     * @Description //TODO 判断是否可以增加图书管理员
     * @Date 23:56 2018/9/11
     * @Param [baname]
     **/
    public int queryCanAdd(String baname) {
        CallableStatement statement = null;
        int retValue = 0;
        try {
            statement = SessionFactoryUtils.getDataSource(sessionFactory)
                    .getConnection().prepareCall("{call queryadbkcanadd(?,?)}");
            statement.setString(1, baname);
            statement.registerOutParameter(2, OracleType.NUMBER);
            statement.execute();

            retValue = statement.getInt(2);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            return retValue;
        }
    }

    /**
     * @return java.util.List<cn.chenforcode.entity.BookAdmin>
     * @Author ChenforCode
     * @Description //TODO 得到所有的图书管理员
     * @Date 0:01 2018/9/12
     * @Param []
     **/
    public List<BookAdmin> getAllBookAdmins() {
        return this.getSession().createQuery("from BookAdmin").list();
    }

    /**
     * @return cn.chenforcode.entity.BookAdmin
     * @Author ChenforCode
     * @Description //TODO 通过图书管理员账号获取图书管理员对象
     * @Date 0:34 2018/9/12
     * @Param [baname]
     **/
    public BookAdmin getBookAdminByBaname(String baname) {
        return (BookAdmin) this.getSession().createQuery("from BookAdmin where baname = ?1")
                .setParameter(1, baname).uniqueResult();
    }

    /**
     * @return void
     * @Author ChenforCode
     * @Description //TODO 删除书籍管理员
     * @Date 11:50 2018/9/13
     * @Param [baname]
     **/
    public void delBookAdmin(String baname) {
        this.getSession().createQuery("delete BookAdmin where baname = ?1")
                .setParameter(1, baname).executeUpdate();
    }

    /**
     * @return void
     * @Author ChenforCode
     * @Description //TODO 更新书籍管理员
     * @Date 12:51 2018/9/13
     * @Param [bookAdmin]
     **/
    public void upBookAdmin(BookAdmin bookAdmin) {
        this.getSession().update(bookAdmin);
    }
}
